<template>
  <div class="cbox">
    <p style="text-align: right; padding: 5px">
      <a-button @click="exportExcel" type="primary" link
        ><template #icon>
          <file-excel-outlined />
        </template>
        导出</a-button
      >
    </p>
    <div class="summary">
      <table>
        <tr>
          <template v-for="(col, i) in state.summary" :key="i">
            <th>{{ col.label }}</th>
            <td>{{ col.value.toFixed(4) }}</td>
          </template>
        </tr>
      </table>
    </div>
    <a-table
      v-if="state.data?.rows"
      :data-source="planList(state.data.rows)"
      :columns="state.columns"
      stripe
      bordered
      size="small"
      :pagination="false"
    >
    </a-table>
  </div>
</template>
<script setup>
import { getCurrentInstance, reactive, ref, watch, onMounted } from "vue";
import { workColumns, showWorkLabel } from "@/utils/workflow";
import _ from "lodash";
import { planList } from "@/utils/planList";

const { proxy } = getCurrentInstance();

const props = defineProps(["work"]);
const state = reactive({
  columns: [],
  data: null,
  summary: [],
});

// 根据工序在二级界面摘取显示字段
const pickColumns = (work, columns) => {
  let pickArray = [];
  if (work === "0") {
    pickArray = [
      "cutArea",
      "length",
      "width",
      "startPower",
      "endPower",
      "startO2",
      "endO2",
      "startAir",
      "endAir",
    ];
  } else if (work === "1") {
    pickArray = ["date", "cutNum", "startGas", "endGas", "startO2", "endO2"];
  } else if (work === "2") {
    pickArray = [
      "startTime",
      "endTime",
      "machineNum",
      "startCO2",
      "endCO2",
      "startO2",
      "endO2",
    ];
  } else if (work === "4-0") {
    pickArray = ["project", "startAir", "endAir", "startGas", "endGas"];
  } else if (work === "4-1") {
    pickArray = ["startTime", "endTime", "startAir", "endAir"];
  } else if (work === "5-0") {
    pickArray = ["startAir", "endAir"];
  } else if (work === "5-1") {
    pickArray = [
      "startTime",
      "endTime",
      "startAir",
      "endAir",
      "airByOne",
      "num",
    ];
  }
  return _.filter(columns, (o) => !pickArray.includes(o.key));
};

// 二级界面API集合
const apis = {
  0: "/calibration/stat/ship_cut/second",
  1: "/calibration/stat/ship_old/second",
  2: "/calibration/stat/ship_weld/second",
  "3-0": "/calibration/stat/machine_pipe/second?type=法兰",
  "3-1": "/calibration/stat/machine_pipe/second?type=弯头",
  "4-0": "/calibration/stat/paint_house/second",
  "4-1": "/calibration/stat/paint_ship/second",
  "5-0": "/calibration/stat/sand_house/second",
  "5-1": "/calibration/stat/sand_ship/second",
  "6-0": "/calibration/stat/water_clean_point/second",
  "6-1": "/calibration/stat/water_clean/second",
  "6-2": "/calibration/stat/water_clean_rust/second",
  "6-3": "/calibration/stat/water_clean_rust_ship/second",
};

watch(
  () => props.work,
  (work) => {
    const v = work;
    // 根据工序获取表单项和数据
    const columns = workColumns[v];
    state.columns = _.map(pickColumns(v, columns), (o) => ({
      ...o,
      ellipsis: true,
    }));

    proxy.$axios.get(apis[v]).then(({ success, data }) => {
      if (success) {
        state.data = data;

        const stat = data.stat;
        if (v === "0") {
          state.summary = [
            { label: "单位能耗", value: stat.powerUnit },
            { label: "单位耗氧", value: stat.o2Unit },
            { label: "单位耗气", value: stat.airUnit },
            { label: "单位压缩空气量耗电量标定值", value: stat.unitAirPower },
            {
              label: "单位耗电能标定值(压缩空气转化)",
              value: stat.powerAirUnit,
            },
          ];
        } else if (v === "1") {
          state.summary = [
            { label: "单位耗天然气量标定值", value: stat.gasByUnit },
            { label: "单位耗氧气量标定值", value: stat.o2ByUnit },
          ];
        } else if (v === "2") {
          state.summary = [
            { label: "单位耗电量标定值", value: stat.powerByUnit },
            { label: "单位耗CO2量标定值", value: stat.co2ByUnit },
          ];
        } else if (v === "3-0") {
          state.summary = [
            {
              label: "法兰氩弧焊接单位用电量标定值",
              value: stat.falanYahuPowerByUnit,
            },
            {
              label: "法兰CO2焊接单位用电量标定值",
              value: stat.falanCo2PowerByUnit,
            },
            {
              label: "法兰管子焊接单位用电量标定值",
              value: stat.falanPowerByUnit,
            },
          ];
        } else if (v === "3-1") {
          state.summary = [
            {
              label: "弯头管子焊接单位用电量标定值",
              value: stat.wantouYahuPowerByUnit,
            },
          ];
        } else if (v === "4-0") {
          state.summary = [
            {
              label: "单位耗电标定值",
              value: stat.unitAirPower,
            },
            {
              label: "单位用压缩空气量标定值",
              value: stat.paintHouseAirByUnit,
            },
            {
              label: "单位用天然气量标定值",
              value: stat.paintHouseGasByUnit,
            },
            {
              label: "单位压缩空气量耗电量标定值",
              value: stat.paintHouseAirPowerByUnit,
            },
            {
              label: "单位耗电能标定值",
              value: stat.paintHousePowerByUnit,
            },
          ];
        } else if (v === "4-1") {
          state.summary = [
            {
              label: "单位用压缩空气量标定值",
              value: stat.unitAirPower,
            },
            {
              label: "单位压缩空气量耗电量标定值",
              value: stat.paintShipAirByUnit,
            },
            {
              label: "单位耗电能标定值",
              value: stat.paintShipAirPowerByUnit,
            },
          ];
        } else if (v === "5-0") {
          state.summary = [
            {
              label: "单位耗电量标定值",
              value: stat.houseSandPowerByUnit,
            },
            {
              label: "单位用压缩空气量标定值",
              value: stat.houseSandAirByUnit,
            },
          ];
        } else if (v === "5-1") {
          state.summary = [
            {
              label: "单位用压缩空气量标定值",
              value: stat.unitAirPower,
            },
            {
              label: "单位压缩空气量耗电量标定值",
              value: stat.shipSandAirByUnit,
            },
            {
              label: "单位耗电能标定值",
              value: stat.shipSandAirPowerByUnit,
            },
          ];
        } else if (v === "6-0") {
          state.summary = [
            {
              label: "超水清洁点打单位耗电标定值",
              value: stat.waterPointPowerByUnit,
            },
          ];
        } else if (v === "6-1") {
          state.summary = [
            {
              label: "超水清洁单位耗电标定值",
              value: stat.waterPowerByUnit,
            },
          ];
        } else if (v === "6-2") {
          state.summary = [
            {
              label: "超水除锈单位耗电标定值",
              value: stat.waterRustPowerByUnit,
            },
          ];
        } else if (v === "6-3") {
          state.summary = [
            {
              label: "舱盖超水除锈单位耗电标定值",
              value: stat.waterRustShipPowerByUnit,
            },
          ];
        }
      } else {
        state.data = null;
      }
    });
  },
  { immediate: true }
);

const excel_apis = {
  // 船体下料
  0: "/excel/export/calibration/stat/ship_cut/second",
  // 船体割旧
  1: "/excel/export/calibration/stat/ship_old/second",
  // 船体焊接
  2: "/excel/export/calibration/stat/ship_weld/second",
  // 法兰管子焊接
  "3-0": "/excel/export/calibration/stat/machine_pipe/fa/second",
  // 弯头管子焊接
  "3-1": "/excel/export/calibration/stat/machine_pipe/wan/second",
  // 喷漆房分段喷漆
  "4-0": "/excel/export/calibration/stat/paint_house/second",
  // 舱室及船壳喷漆
  "4-1": "/excel/export/calibration/stat/paint_ship/second",
  // 打砂房分段打砂
  "5-0": "/excel/export/calibration/stat/sand_house/second",
  // 舱室打砂
  "5-1": "/excel/export/calibration/stat/sand_ship/second",
  // 超水清洁点打
  "6-0": "/excel/export/calibration/stat/water_clean_point/second",
  // 超水清洁
  "6-1": "/excel/export/calibration/stat/water_clean/second",
  // 超水除锈
  "6-2": "/excel/export/calibration/stat/water_clean_rust/second",
  // 舱盖超水除锈
  "6-3": "/excel/export/calibration/stat/water_clean_rust_ship/second",
};

const exportExcel = () => {
  const url = excel_apis[props.work];
  const name = showWorkLabel(props.work);
  proxy.$axios
    .get(url, {
      responseType: "blob",
    })
    .then((data) => {
      const blobUrl = window.URL.createObjectURL(data);
      const link = document.createElement("a");
      link.href = blobUrl;
      link.download = `${name}.xlsx`;
      document.body.appendChild(link);
      link.click();
      document.body.removeChild(link);
      window.URL.revokeObjectURL(blobUrl);
    });
};
</script>
<style lang="less" scoped>
.cbox {
  width: 100%;
  height: 100%;
  padding: 2px;

  .summary {
    table {
      width: 100%;
      color: #fff;

      th,
      td {
        border: 1px solid gray;
        text-align: center;
        padding: 4px;
        font-size: 12px;
        min-width: 50px;
      }

      th {
        background-color: #141414;
      }

      td {
        color: red;
        font-size: 16px;
        background-color: #141414;
      }
    }
  }
}
</style>
